package org.jeecg.modules.job.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.job.cms.entity.*;
import org.jeecg.modules.job.cms.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 内容管理模块
 */
@RestController
@RequestMapping("/api/cms")
@Api(tags="移动端内容管理模块")
@Slf4j
public class CmsController {
	@Autowired
	private org.jeecg.modules.job.cms.service.ICmsArticlesService articlesService;
	@Autowired
	private org.jeecg.modules.job.cms.service.ICmsCategoryService categoryService;
	@Autowired
	private org.jeecg.modules.job.cms.service.ICmsFeedbackService feedbackService;
	@Autowired
	private org.jeecg.modules.job.cms.service.ICmsNoticeService noticeService;
	@Autowired
	private ICmsAdService adService;
	@Autowired
	private ICmsNewsService newsService;
	@Autowired
	private ICmsTypeService typeService;
	@Autowired
	private ICmsAboutUsService aboutUsService;
	@Autowired
	private ICmsContactUsService contactUsService;


	/**
	 * 内容查询接口（查询详情）
	 *
	 * @param categoryCode
	 * @return
	 */
	@ApiOperation("内容查询接口")
	@GetMapping(value = "/getArticle")
	public Result<Object> getArticle(@RequestParam("categoryCode") String categoryCode) {
		if(oConvertUtils.isEmpty(categoryCode)){
			return Result.error("分类编号不能为空");
		}
		try {
			String ids= categoryService.queryChildIds(categoryCode);
			QueryWrapper queryWrapper=new QueryWrapper();
			List<String> newIds= Arrays.asList(ids.split(","));
			queryWrapper.in("category_id",newIds);
			queryWrapper.orderByAsc("sort");
			List<CmsArticles> list=articlesService.list(queryWrapper);
			if(list.size()>0){
				return Result.ok(list.get(0));
			}
			return Result.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 内容列表查询接口（无详情，用于列表展示）
	 *
	 * @param categoryCode
	 * @return
	 */
	@ApiOperation("内容列表查询接口")
	@GetMapping(value = "/getArticleList")
	public Result<Object> getArticleList(@RequestParam("categoryCode") String categoryCode) {
		if(oConvertUtils.isEmpty(categoryCode)){
			return Result.error("分类编号不能为空");
		}
		try {
			String ids= categoryService.queryChildIds(categoryCode);
			QueryWrapper queryWrapper=new QueryWrapper();
			List<String> newIds= Arrays.asList(ids.split(","));
			queryWrapper.in("category_id",newIds);
			queryWrapper.orderByAsc("sort");
			queryWrapper.select("id,title,category_id,avatar,banner");
			List<CmsArticles> list=articlesService.list(queryWrapper);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 子分类查询接口
	 *
	 * @param categoryCode
	 * @return
	 */
	@ApiOperation("子分类查询接口")
	@GetMapping(value = "/getCategoryChilds")
	public Result<Object> getCategoryList(@RequestParam("categoryCode") String categoryCode) {
		if(oConvertUtils.isEmpty(categoryCode)){
			return Result.error("分类编号不能为空");
		}
		try {
			List<CmsCategory> list= categoryService.getCategoryChilds(categoryCode);
			return Result.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 添加意见反馈
	 *
	 * @param cmsFeedback
	 * @return
	 */
	@ApiOperation("添加意见反馈")
	@PostMapping(value = "/addFeedback")
	public Result<Object> addFeedback(@RequestBody org.jeecg.modules.job.cms.entity.CmsFeedback cmsFeedback) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		cmsFeedback.setUserId(user.getId());
		if(oConvertUtils.isEmpty(cmsFeedback.getContent())){
			return Result.error("反馈内容不能为空");
		}
		try {
			boolean result=feedbackService.save(cmsFeedback);
			return Result.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 查询公告列表
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询公告列表")
	@GetMapping(value = "/getNoticeList")
	public Result<IPage<CmsNotice>> getNoticeList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                  @RequestParam(name="pageSize", defaultValue="20") Integer pageSize) {
		Page<CmsNotice> page = new Page<>(pageNo, pageSize);
		IPage<CmsNotice> pageList = noticeService.page(page,new QueryWrapper<>(new org.jeecg.modules.job.cms.entity.CmsNotice().setStatus(1)).orderByDesc("set_top").orderByDesc("create_time"));
		return Result.OK(pageList);
	}



	/**
	 * 查询通知详情
	 *
	 * @param id
	 * @return
	 */
	@ApiOperation("查询通知详情")
	@GetMapping(value = "/getNoticeDetail")
	public Result<Object> getNoticeDetail(@RequestParam("id") String id) {
		try {
			org.jeecg.modules.job.cms.entity.CmsNotice notice=noticeService.getById(id);
			return Result.ok(notice);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.error(e.getMessage());
		}
	}

	/**
	 * 查询广告列表
	 *
	 * @return
	 */
	@ApiOperation(value="查询广告列表")
	@GetMapping(value = "/getAdList")
	public Result<List<CmsAd>> getAdList() {
		List<CmsAd> pageList = adService.list(new QueryWrapper<>(new CmsAd().setStatus(1)).orderByAsc("sort"));
		return Result.OK(pageList);
	}

	/**
	 * 查询社科动态列表
	 *
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value="查询社科动态列表")
	@GetMapping(value = "/getNewsList")
	public Result<IPage<CmsNews>> getNewsList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											  @RequestParam(name="pageSize", defaultValue="20") Integer pageSize) {
		Page<CmsNews> page = new Page<>(pageNo, pageSize);
		IPage<CmsNews> pageList = newsService.page(page,new QueryWrapper<>(new CmsNews().setStatus(1)).orderByDesc("publish_time"));
		return Result.OK(pageList);
	}

	/**
	 * 查询首页分类列表
	 *
	 * @return
	 */
	@ApiOperation(value="查询首页分类列表")
	@GetMapping(value = "/getTypeList")
	public Result<List<CmsType>> getTypeList() {
		List<CmsType> pageList = typeService.list(new QueryWrapper<>(new CmsType().setStatus(1)).orderByAsc("sort"));
		return Result.OK(pageList);
	}


	/**
	 * 关于我们
	 */
	@RequestMapping(value = "/aboutUs/detail",method = RequestMethod.GET)
	@ApiOperation(value = "关于我们", notes = "关于我们")
	public Result aboutUsDetail() {
		List<CmsAboutUs> results=aboutUsService.list(new QueryWrapper<>(new CmsAboutUs()));
		if (results.size()>0){
			return Result.ok(results.get(0));
		}
		return Result.ok();
	}

	/**
	 * 联系我们
	 */
	@RequestMapping(value = "/contactUs/detail",method = RequestMethod.GET)
	@ApiOperation(value = "联系我们", notes = "联系我们")
	public Result contactUs() {
		List<CmsContactUs> results=contactUsService.list(new QueryWrapper<>(new CmsContactUs()));
		if (results.size()>0){
			return Result.ok(results.get(0));
		}
		return Result.ok();
	}


}